package com.li.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.mapper.PermissionsMapper;
import com.li.pojo.Permission;
import com.li.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service(interfaceClass = PermissionsService.class)
@Transactional
public class PermissionsServiceImpl implements PermissionsService {
    @Autowired
    PermissionsMapper permissionsMapper;
    @Override
    public List<Permission> findAll() {
        return permissionsMapper.findAll();
    }

    public void add(Permission permission) {
        permissionsMapper.add(permission);

    }

    public void deleteById(Integer id) {
        permissionsMapper.deleteById(id);

    }

    public void updateById(Permission permission) {
        permissionsMapper.updateById(permission);
    }

    public Permission findById(Integer id) {
        return permissionsMapper.findById(id);
    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        // 设置分页
        PageHelper.startPage(currentPage,pageSize);
        // 查询数据库 并获取total 以及权限集合
        Page<Permission> pages = permissionsMapper.findAllByQueryString(queryPageBean.getQueryString());
        long total = pages.getTotal();
        List<Permission> permissionList = pages.getResult();
        // 返回结果
        return new PageResult(total, permissionList);
    }

    @Override
    public List<Integer> findIdsByRoleId(Integer roleId) {

        return permissionsMapper.findIdsByRoleId(roleId);
    }
}
